//
//  GCWokDetailViewController.m
//  GoldChampion
//
//  Created by Mac on 2018/4/13.
//  Copyright © 2018年 stoneobs@icloud.com. All rights reserved.
//

#import "GCWokDetailViewController.h"
#import "GCWorkDetailNavView.h"
#import "GCOtherHomeViewController.h"
#import "GCCommentHeaderView.h"
#import "GCCommentReplayTableViewCell.h"
#import "LGCultureCommentPresentController.h"
#import "GCSharePresentViewController.h"
#import "GCReportViewController.h"
#import "CGPanTranceationAnimation.h"
#import "GCTranceationingAnimation.h"
@interface GCWokDetailViewController ()<STActionSheetDelegate,UINavigationControllerDelegate>
@property(nonatomic, strong) GCWorkDetailNavView                     *navView;
@property(nonatomic, strong) CGPanTranceationAnimation                     *panAnimationObj;
@property(nonatomic, strong) GCTranceationingAnimation                     *animationObj;
@end

@implementation GCWokDetailViewController

#pragma mark --vc 生命周期
- (void)viewDidLoad{
    [super viewDidLoad];
    [self configSubView];
    [self configTableHeaderView];
    [self configFooterAndBottomView];
    self.navView.alpha = 0;
    
    self.navigationController.delegate = self;
    self.panAnimationObj = [CGPanTranceationAnimation new];
    self.animationObj = [GCTranceationingAnimation new];
    self.animationObj.pop = YES;
    
    [self.panAnimationObj wireToViewController:self];
    self.imageView.userInteractionEnabled = YES;
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pop)];
    [self.imageView addGestureRecognizer:gesture];
    
    

    
}
- (void)pop{
    self.panAnimationObj = nil;
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self st_hideNavagetionbarAnimated:NO];
    
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [UIView animateWithDuration:0.25 animations:^{
        self.navView.alpha = 1;
    }];
}
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
     [self st_showNavagationbarAnimated:NO];
}
- (void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.navView = [[GCWorkDetailNavView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 0)];
    [self.navView setOnSelectedMenuHandle:^(NSInteger index) {
        [weakSelf onSelctedHeaderMenuWithIndex:index];
    }];
    [self.view addSubview:self.navView];
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (ios11 && [self.tableView respondsToSelector:@selector(setContentInsetAdjustmentBehavior:)]) {
        [self.tableView setContentInsetAdjustmentBehavior:@(2)];
    }
    self.tableView.contentInset = UIEdgeInsetsZero;
    self.tableView.showsVerticalScrollIndicator = NO;
    
    
    [self.tableView registerClass:[GCCommentHeaderView class] forHeaderFooterViewReuseIdentifier:@"header"];
    [self.tableView registerClass:[GCCommentReplayTableViewCell class] forCellReuseIdentifier:@"cell"];
    
}
- (UIImageView *)imageView{
    if (!_imageView) {
        _imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, UIScreenHeight * 0.6)];
        _imageView.contentMode = UIViewContentModeScaleAspectFill;
        _imageView.clipsToBounds  = YES;
    }
    return _imageView;
}
- (STButton*)defultButtonWithTitle:(NSString*)title imageName:(NSString*)imageName attributedText:(NSString*)attributedText{
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(self.insetX, 0, UIScreenWidth - self.insetX, 35)
                                                     title:title
                                                titleColor:SecendTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:[UIImage imageNamed:imageName]];
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    if (attributedText.length) {
        NSAttributedString * string = [buyButton.currentTitle st_convertAttributeStringWithKeyWord:attributedText attributes:@{NSForegroundColorAttributeName:UIColorFromRGBA(0xFF9000)}];
        [buyButton setAttributedTitle:string forState:UIControlStateNormal];
    }
    return buyButton;
}
- (void)configTableHeaderView{
    UIView * header = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 20)];
    [header addSubview:self.imageView];
    

    
    
    STLabel * playLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, self.imageView.bottom, 200, 44)
                                                     text:@"300次播放"
                                                textColor:SecendTextColor
                                                     font:14
                                              isSizetoFit:NO
                                            textAlignment:NSTextAlignmentLeft];
    [header addSubview:playLable];
    
    STLabel * timeLable = [[STLabel alloc] initWithFrame:CGRectMake(self.insetX, self.imageView.bottom, 200, 44)
                                                    text:@"2小时前"
                                               textColor:SecendTextColor
                                                    font:14
                                             isSizetoFit:NO
                                           textAlignment:NSTextAlignmentRight];
    timeLable.right = UIScreenWidth - self.insetX;
    [header addSubview:timeLable];
    
    STButton * musicButton = [self defultButtonWithTitle:@"星星" imageName:@"音乐" attributedText:@"星星"];
    musicButton.top = playLable.bottom + 5;
    [header addSubview:musicButton];
    
    STButton * volueButton = [self defultButtonWithTitle:@"大佬:又是美丽的一天" imageName:@"通知2" attributedText:@"大佬:"];
    volueButton.top = musicButton.bottom ;
    [header addSubview:volueButton];
    
    STButton * likeButton = [self defultButtonWithTitle:@"1234喜欢" imageName:@"关注hei" attributedText:@""];
    likeButton.top = volueButton.bottom ;
    [header addSubview:likeButton];
    
    STButton * commentButton = [self defultButtonWithTitle:@"353评论" imageName:@"评论" attributedText:@""];
    commentButton.top = likeButton.bottom ;
    [header addSubview:commentButton];
    
    UIView * line = [[UIView alloc] initWithFrame:CGRectMake(0, commentButton.bottom + 15, UIScreenWidth, 0.5)];
    line.backgroundColor = TM_lineColor;
    [header addSubview:line];
    header.height = line.bottom ;
    self.tableView.tableHeaderView = header;
}
- (void)configFooterAndBottomView{
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    self.tableView.tableFooterView = footer;
    __weak typeof(self) weakSelf =  self;
    UIView * bootomView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 49)];
    bootomView.top = [TMUtils tabBarTop];
    bootomView.backgroundColor = [UIColor whiteColor];
    [bootomView st_showTopShadow];
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(0, 1, UIScreenWidth, 48)
                                                     title:@"说点什么吧"
                                                titleColor:SecendTextColor
                                                 titleFont:14
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    buyButton.titleEdgeInsets = UIEdgeInsetsMake(0, 10, 0, 0);
    [bootomView addSubview:buyButton];
    [buyButton setClicAction:^(UIButton *sender) {
        LGCultureCommentPresentController * vc = [LGCultureCommentPresentController new];
        [vc setOnSelctedSendHandle:^(NSString *text) {
            
        }];
        [weakSelf presentViewController:vc animated:NO completion:nil];
    }];
    [self.view addSubview:bootomView];
}

#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 10;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [GCCommentReplayTableViewCell cellHeight];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return [GCCommentHeaderView headerHeight];
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    GCCommentHeaderView * header = (id)[tableView dequeueReusableHeaderFooterViewWithIdentifier:@"header"];
    return header;
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    GCCommentReplayTableViewCell * cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"];
    return cell;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [self.navView scrollViewDidScroll:scrollView];
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSelctedHeaderMenuWithIndex:(NSInteger)index{
    if (index == 0) {
        [self pop];
    }
    if (index == 1) {
        STActionSheet *sheet = [STActionSheet actionSheetWithTitle:nil buttonTitles:@[@"举报",@"减少类似作品",@"取消关注"] cancelTitle:@"取消" delegate:self];
        [sheet setButtonColor:FlatBlue index:0];
        [sheet setButtonColor:FlatBlue index:1];
        [sheet setButtonColor:FlatBlue index:2];
        [sheet.cancleButton setTitleColor:FlatBlue forState:UIControlStateNormal];
        [sheet show];
    }
    if (index == 2) {
        [self.navigationController pushViewController:[GCOtherHomeViewController new] animated:YES];
    }
    if (index == 3) {
        GCSharePresentViewController *vc = [GCSharePresentViewController new];
        [vc setOnSelctedShareHandle:^(NSString *shareName) {
            
        }];
        [self presentViewController:vc animated:NO completion:nil];
    }
    if (index == 4) {
        [self.navigationController pushViewController:[GCOtherHomeViewController new] animated:YES];
    }
}
- (void)st_leftBarAction:(id)sender{
   
}
#pragma mark --UINavigationControllerDelegate
- (nullable id <UIViewControllerInteractiveTransitioning>)navigationController:(UINavigationController *)navigationController
                                   interactionControllerForAnimationController:(id <UIViewControllerAnimatedTransitioning>) animationController NS_AVAILABLE_IOS(7_0){
    
    return self.panAnimationObj;
}
- (id<UIViewControllerAnimatedTransitioning>)navigationController:(UINavigationController *)navigationController animationControllerForOperation:(UINavigationControllerOperation)operation fromViewController:(UIViewController *)fromVC toViewController:(UIViewController *)toVC{
    
    if (fromVC == self && toVC == self.navigationController.childViewControllers.firstObject) {
       return self.animationObj;
    }
    return nil;
}

#pragma mark --STActionSheetDelegate
- (void)actionSheet:(STActionSheet *)actionSheet didClickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        [self.navigationController pushViewController:[GCReportViewController new] animated:YES];
    }
    if (buttonIndex == 1) {
        [SVProgressHUD showInfoWithStatus:@"以后将减少该类作品推荐"];
    }
    if (buttonIndex == 2) {
        
    }
}
@end
